package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.service.WorkReportSetService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.workreportset.pojo.dto.WorkReportSetDetailDTO;
import com.xbongbong.pro.workreportset.pojo.dto.WorkReportSetSaveDTO;
import com.xbongbong.pro.workreportset.pojo.vo.WorkReportSetDetailVO;
import com.xbongbong.pro.workreportset.pojo.vo.WorkReportSetFormVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author youli.chen
 * @ProjectName xbb-pro
 * @Description: 工作报告设置相关数据接口
 * @date 2019/1/10 9:51
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/workReport/set")
public class WorkReportSetController {

    @Resource
    private WorkReportSetService workReportSetService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    /**
     * 获取工作报告设置
     * @param workReportSetDetailDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getConfig", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getConfig(@RequestBody @Valid WorkReportSetDetailDTO workReportSetDetailDTO, BindingResult br){
        XbbResponse<WorkReportSetDetailVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(workReportSetDetailDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(workReportSetDetailDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                response = this.workReportSetService.workReportSet(workReportSetDetailDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 保存工作报告设置
     * @param workReportSetSaveDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/saveConfig", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String saveConfig(@RequestBody @Valid WorkReportSetSaveDTO workReportSetSaveDTO, BindingResult br) {
        XbbResponse<Integer> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(workReportSetSaveDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(workReportSetSaveDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                response = this.workReportSetService.workReportSetSave(workReportSetSaveDTO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 工作报告表单（日，周，月）
     * @param baseDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/formList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String formList(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<WorkReportSetFormVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkReportSetFormVO workReportSetFormVO = workReportSetService.formList(baseDTO);
                response = new XbbResponse<>(workReportSetFormVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
